import config from './config.js';
/**
 * 判断是否为空
 * @param {Any} e 
 */
export function empty(e) {
	var type = true;
	if (e != '' && e != null && e != 'null' && typeof(e) != 'undefined') {
		type = false;
	}
	return type;
}

export function openPages(path, datas) {
	if (this.$empty(path)) {
		this.$toast('该功能开发中')
		return;
	}
	if (this.$empty(datas)) {
		uni.navigateTo({
			url: path,
		});
	} else {
		uni.navigateTo({
			url: path + '?' + datas,
		});
	}
}
/**
 * 弹窗
 * @param {String} msg 
 * @param {String} icon
 */
export function showToast(msg, icon) {
	if (!msg) {
		title = '温馨提示';
	}
	uni.showToast({
		title: msg,
		icon: icon,
		duration: 1500
	});
}
/**
 * 模态框
 * @param {String} title
 * @param {String} msg
 */
export function showModal(title, msg) {
	return new Promise((resolve, reject) => {
		if (!title) {
			title = '温馨提示';
		}
		uni.showModal({
			title: title,
			content: msg,
			cancelText: '取消',
			confirmText: '确认',
			mask: true,
			success: function(res) {
				if (res.confirm) {
					resolve();
				} else if (res.cancel) {
					reject();
				}
			}
		});
	})
}
/**
 * 轻提示
 * @param {String} title 提示内容
 */
export function toast(title) {
	const txt = String(title);
	if (!txt) {
		return false;
	}
	uni.showToast({
		icon: 'none',
		title: txt
	})
}
/**
 * 关闭页面
 * @param {Object} num
 */
export function closePage(num) {
	uni.navigateBack({
		delta: num
	})
}
/**
 * 拨打电话
 * @param {Object} phone
 */
export function callPhone(phone) {
	if (empty(phone)) {
		console.log('没得号码')
		return;
	}
	uni.makePhoneCall({
		phoneNumber: phone //仅为示例
	});
}

/**
 * 选择图片-相机/相册
 */
export function chooseImgs(count = 1) {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: count, // 相册下的可选图片个数
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 从相册选择或相机拍摄
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	}).then(res => {
		return [null, res.tempFilePaths]
	}).catch(err => {
		toast('选择图片错误！')
		return [err]
	})
}

/**
 * 预览图片
 */
export function previewImg(obj = {}) {
	let {current, urls, indicator, loop, longPressActions} = obj
	if (!urls) return
	current = current || 0
	indicator = indicator || 'number'
	loop = loop || false
	longPressActions = longPressActions || {}
	return new Promise((resolve, reject) => {
		uni.previewImage({
			current, // 当前要显示图片的链接/索引值
			urls, // 预览图片的链接列表
			indicator, // "default"底部圆点,"number"顶部数字
			loop, // 是否可循环预览
			longPressActions,  // 长按菜单，默认为保存相册
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	}).then(res => {
		return [null, res]
	}).catch(err => {
		return [err]
	})
}

/**
 * 上传图片
 * @param {Object} path
 */
export function uploadImg(path) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: config.host + config.upload_img, //仅为示例，非真实的接口地址
			filePath: path,
			name: 'file',
			formData: {
				'file': path
			},
			success: (uploadFileRes) => {
				console.log(uploadFileRes)
				resolve(uploadFileRes.data);
			},
			fail: (err) => {
				console.log(err)
				reject(err);
			}
		})
	})
}
